<template>
  <div class="cart-page">
    <Header />
    <div class="container">
      <div class="cart-header">
        <h2 class="title">我的购物车 <span class="course-count">共1门课程</span></h2>
      </div>
      
      <div class="cart-content">
        <div class="cart-table">
          <div class="table-header">
            <label class="checkbox-wrapper">
              <input type="checkbox" v-model="selectAll" @change="handleSelectAll">
              <span class="checkbox-label">全选</span>
            </label>
            <span class="header-item course">课程</span>
            <span class="header-item validity">有效期</span>
            <span class="header-item price">单价</span>
            <span class="header-item action">操作</span>
          </div>
          
          <div class="cart-items">
            <div v-for="item in cartItems" :key="item.id" class="cart-item">
              <label class="checkbox-wrapper">
                <input type="checkbox" v-model="item.selected">
              </label>
              <div class="course-info">
                <img :src="item.image" :alt="item.title" class="course-image">
                <div class="course-detail">
                  <h3 class="course-title">{{ item.title }}</h3>
                  <p class="course-desc">{{ item.description }}</p>
                </div>
              </div>
              <div class="validity">
                <select v-model="item.validity" class="validity-select">
                  <option value="forever">永久有效</option>
                  <option value="1year">1年有效</option>
                  <option value="6months">6个月有效</option>
                </select>
              </div>
              <div class="price">
                <span class="current-price">¥{{ item.price }}</span>
                <span class="original-price" v-if="item.originalPrice">¥{{ item.originalPrice }}</span>
              </div>
              <div class="action">
                <button class="delete-btn" @click="removeItem(item)">删除</button>
              </div>
            </div>
          </div>
        </div>
        
        <div class="cart-footer">
          <div class="left">
            <label class="checkbox-wrapper">
              <input type="checkbox" v-model="selectAll" @change="handleSelectAll">
              <span class="checkbox-label">全选</span>
            </label>
            <button class="batch-delete-btn" @click="batchDelete">批量删除</button>
          </div>
          <div class="right">
            <div class="total">
              合计：<span class="total-price">¥{{ totalPrice }}</span>
            </div>
            <button class="checkout-btn" :disabled="!hasSelectedItems" @click="goToCheckout">
              去结算 ({{ selectedCount }})
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import Header from '../components/Header.vue'

const router = useRouter()

const cartItems = ref([
  {
    id: 1,
    title: 'Python开发21天入门',
    description: '专属新手21天零基础入门人工智能领域的必修课',
    image: 'https://img3m2.ddimg.cn/74/31/29668592-1_h_1705747719.jpg',
    price: 9.00,
    originalPrice: 9.00,
    validity: 'forever',
    selected: true
  }
])

const selectAll = computed({
  get: () => cartItems.value.every(item => item.selected),
  set: (value) => cartItems.value.forEach(item => item.selected = value)
})

const totalPrice = computed(() => {
  return cartItems.value
    .filter(item => item.selected)
    .reduce((sum, item) => sum + item.price, 0)
    .toFixed(2)
})

const selectedCount = computed(() => {
  return cartItems.value.filter(item => item.selected).length
})

const hasSelectedItems = computed(() => selectedCount.value > 0)

const handleSelectAll = () => {
  const newValue = !selectAll.value
  cartItems.value.forEach(item => item.selected = newValue)
}

const removeItem = (item) => {
  const index = cartItems.value.indexOf(item)
  if (index > -1) {
    cartItems.value.splice(index, 1)
  }
}

const batchDelete = () => {
  cartItems.value = cartItems.value.filter(item => !item.selected)
}

const goToCheckout = () => {
  const selectedItems = cartItems.value.filter(item => item.selected)
  router.push({
    path: '/checkout',
    query: {
      items: selectedItems.map(item => item.id).join(','),
      total: totalPrice.value
    }
  })
}
</script>

<style scoped>
.cart-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding-top: 64px;
}

.cart-header {
  margin-bottom: 20px;
}

.title {
  font-size: 20px;
  color: #333;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 12px;
}

.course-count {
  font-size: 14px;
  color: #999;
  font-weight: normal;
}

.cart-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.table-header {
  display: grid;
  grid-template-columns: 80px minmax(400px, 1fr) 200px 150px 120px;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #f0f0f0;
  color: #666;
  background: #fafafa;
}

.header-item {
  text-align: center;
}

.header-item.course {
  text-align: left;
  padding-left: 80px;
}

.cart-item {
  display: grid;
  grid-template-columns: 80px minmax(400px, 1fr) 200px 150px 120px;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #f0f0f0;
}

.course-info {
  display: flex;
  gap: 20px;
  min-width: 0;
  padding-left: 60px;
}

.course-image {
  width: 160px;
  height: 90px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}

.course-detail {
  flex: 1;
  min-width: 0;
  padding-right: 20px;
}

.course-title {
  font-size: 16px;
  color: #333;
  margin: 0 0 8px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.course-desc {
  font-size: 14px;
  color: #666;
  margin: 0;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.validity {
  text-align: center;
}

.validity-select {
  width: 140px;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 8px;
  color: #333;
  outline: none;
  background: white;
}

.price {
  text-align: center;
}

.action {
  text-align: center;
}

.current-price {
  font-size: 16px;
  color: #ff6b00;
  font-weight: 500;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
  margin-left: 8px;
}

.delete-btn {
  padding: 6px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: white;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
}

.delete-btn:hover {
  color: #ff6b00;
  border-color: #ff6b00;
}

.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: #fafafa;
  border-radius: 0 0 8px 8px;
}

.left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.batch-delete-btn {
  padding: 6px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: white;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
}

.batch-delete-btn:hover {
  color: #ff6b00;
  border-color: #ff6b00;
}

.right {
  display: flex;
  align-items: center;
  gap: 24px;
}

.total {
  font-size: 14px;
  color: #666;
}

.total-price {
  font-size: 20px;
  color: #ff6b00;
  font-weight: bold;
}

.checkout-btn {
  padding: 8px 32px;
  border: none;
  border-radius: 4px;
  background: #ff6b00;
  color: white;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.checkout-btn:hover {
  background: #ff8533;
}

.checkout-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}
</style> 